package com.site.wisdom_construction.controller.statisticsModule;

import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.service.safetyModule.SafetyDangerStatisticsService;
import com.site.wisdom_construction.service.statisticsModule.StatisticsSafetyService;
import com.site.wisdom_construction.statisticsResult.SafetyByHt;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/** @Author daiyifei @Date 2022/2/12 @Description */
@Api(tags = "安全模块统计数据接口")
@RestController
@RequestMapping("StatisticsSafety")
@CrossOrigin
public class StatisticsSafetyController {
  @Autowired private SafetyDangerStatisticsService safetyDangerStatisticsService;

  @Autowired private StatisticsSafetyService statisticsSafetyService;

  @ApiOperation(value = "安全隐患总数据", notes = "测试数据:")
  @PostMapping("/SafetyByHt")
  @NoToken
  public OneDataResult SafetyByHt(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    SafetyByHt safetyByHt = new SafetyByHt();
    // 安全隐患总数
    safetyByHt.setCountSafetyAll(statisticsSafetyService.countSafetyAll(map));
    // 待审核安全隐患数
    safetyByHt.setCountSafetyAllByStatusA(statisticsSafetyService.countSafetyAllByStatusA(map));
    // 无效安全隐患数
    safetyByHt.setCountSafetyAllByStatusW(statisticsSafetyService.countSafetyAllByStatusW(map));
    // 待整改安全隐患数
    safetyByHt.setCountSafetyAllByStatusD(statisticsSafetyService.countSafetyAllByStatusD(map));
    // 已整改安全隐患数
    safetyByHt.setCountSafetyAllByStatusY(statisticsSafetyService.countSafetyAllByStatusY(map));

    Double CountSafetyAll = Double.valueOf(statisticsSafetyService.countSafetyAll(map));
    Double CountSafetyAllByStatusY =
        Double.valueOf(statisticsSafetyService.countSafetyAllByStatusY(map));
    Double d = (CountSafetyAllByStatusY / CountSafetyAll) * 100;

    String workerTrain = String.valueOf(d);
    // 整改率
    if ("NaN".equals(workerTrain) || "0.0".equals(workerTrain)) {
      safetyByHt.setRectificationRate("0");
    } else if ("100.0".equals(workerTrain)) {
      safetyByHt.setRectificationRate("100");
    } else {
      if (workerTrain.length() == 3) {
        safetyByHt.setRectificationRate(workerTrain.substring(0, 3));
      } else if (workerTrain.length() == 4) {
        safetyByHt.setRectificationRate(workerTrain.substring(0, 4));
      } else {
        safetyByHt.setRectificationRate(workerTrain.substring(0, 5));
      }
    }
    oneDataResult.setData(safetyByHt);
    return oneDataResult;
  }

  @ApiOperation(value = "根据安全隐患类型分类统计数据", notes = "测试数据:")
  @PostMapping("/countBySafetyTypeName")
  @NoToken
  public OneDataResult countBySafetyTypeName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countBySafetyTypeName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据劳务队伍分类统计数据", notes = "测试数据:")
  @PostMapping("/countBySubcontractorsName")
  @NoToken
  public OneDataResult countBySubcontractorsName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countBySubcontractorsName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据隐患区域分类统计数据", notes = "测试数据:")
  @PostMapping("/countByDistrictName")
  @NoToken
  public OneDataResult countByDistrictName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countByDistrictName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据班组分类统计数据", notes = "测试数据:")
  @PostMapping("/countByTeamName")
  @NoToken
  public OneDataResult countByTeamName(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countByTeamName(map));
    return oneDataResult;
  }

  @ApiOperation(value = "根据负责人分类统计数据", notes = "测试数据:")
  @PostMapping("/countByResponsiblePerson")
  @NoToken
  public OneDataResult countByResponsiblePerson(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(statisticsSafetyService.countByResponsiblePerson(map));
    return oneDataResult;
  }

  @ApiOperation(value = "当月安全隐患情况分析", notes = "测试数据:")
  @PostMapping("/selectSafetyDangerStatistics")
  @NoToken
  public OneDataResult selectSafetyDangerStatistics(@RequestBody Map map) {
    OneDataResult oneDataResult = new OneDataResult();
    oneDataResult.setData(safetyDangerStatisticsService.selectSafetyDangerStatistics(map));
    return oneDataResult;
  }
}
